
#include "test_tools.h"

#ifdef _DEBUG
#pragma message("debug")
#else
#pragma message("release")
#endif

#if 1
	void replaceAll(std::string& source, const char* replaceWhat, const char* replaceWithWhat)
	{
		std::string::size_type pos = 0;
		std::string::size_type what = strlen(replaceWhat);
		std::string::size_type withWhat = strlen(replaceWithWhat);
		while(1)
		{
			pos = source.find(replaceWhat,pos);

			if (pos == std::string::npos)
				break;

			source.replace(pos,what,replaceWithWhat);

			pos += withWhat;
		}
	}
#endif

int main()
{
	// test
#if 0
	std::deque<int> std_deque;
	Util::Deque<int> util_deque;

	START_PERFORMANCE(1)
	for (int i = 0; i<100000; i++)
	{
		std_deque.push_front(i);
		//std_deque.push_back(i);
	}
	END_PERFORMANCE(1)
	cout <<"--- Std   deque  push_front"<<endl; 

	START_PERFORMANCE(2)
	for (int i = 0; i<100000; i++)
	{
		util_deque.PushFront(i);
		//util_deque.PushBack(i);
	}
	END_PERFORMANCE(2)
	cout <<"--- Util   Deque  PushFront"<<endl; 


#endif

#if 0

	Util::Set<int> set_util;
	std::set<int> set_std;

	START_PERFORMANCE(1)
	set_util.BeginBulkInsert();
	for (int i=0; i<100000; ++i )
	{
		set_util.Insert(100000 - i);
	}
	set_util.EndBulkInsert();
	END_PERFORMANCE(1)
	cout <<"-------- Util  Set  Insert"<<endl; 

	START_PERFORMANCE(2)
	for (int i=0; i<100000; ++i )
	{
		set_std.insert(100000 - i);
	}
	END_PERFORMANCE(2)
	cout <<"-------- std  set  insert"<<endl; 

	START_PERFORMANCE(3)
	for (int i=0; i<100000; ++i )
	{
		set_util.Find(i);
	}
	END_PERFORMANCE(3)
	cout <<"-------- Util  Set  Find"<<endl; 

	START_PERFORMANCE(4)
	for (int i=0; i<100000; ++i )
	{
		set_std.find(i);
	}
	END_PERFORMANCE(4)
	cout <<"-------- std  set  find"<<endl; 

	START_PERFORMANCE(5)
	for (int i=0; i<100000; ++i )
	{
		set_util.Erase(i);
	}
	END_PERFORMANCE(5)
	cout <<"-------- Util  Set  Erase"<<endl; 

	START_PERFORMANCE(6)
	for (int i=0; i<100000; ++i )
	{
		set_std.erase(i);
	}
	END_PERFORMANCE(6)
	cout <<"-------- std  set  erase"<<endl; 

#endif

#if 0
	Util::Deque<int>::type util_deq;
	std::deque<int> std_deq;

	START_PERFORMANCE(1)
	for (int i=0; i<2000000; ++i )
	{
		util_deq.push_back(i);
// 		util_deq.push_back(i);
// 		util_deq.push_front(i);
// 		util_deq.push_front(i);
// 		util_deq.pop_front();
// 		util_deq.pop_back();
	}
	END_PERFORMANCE(1)
	cout <<"-------- Util::Deque   integrated testing "<<endl; 

	START_PERFORMANCE(2)
	for (int i=0; i<2000000; ++i )
	{
		std_deq.push_back(i);
// 		std_deq.push_back(i);
// 		std_deq.push_front(i);
// 		std_deq.push_front(i);
// 		std_deq.pop_front();
// 		std_deq.pop_back();
	}
	END_PERFORMANCE(2)
	cout <<"-------- std::deque   integrated testing"<<endl; 



#endif
	// test integrated performance of Util::BitField
#if 0

	Util::BitField<128> util_bit;
	std::bitset<128> std_bit;

	START_PERFORMANCE(2)
	for (uint i=0; i<5000000; ++i )
	{
		std_bit.set(i%128,true);
		std_bit.set(i%128, false);
		std_bit.test(i%128);
		std_bit.reset();
	}
	END_PERFORMANCE(2)
	cout <<"-------- std::bitset   integrated testing"<<endl; 

	START_PERFORMANCE(1)
	for (uint i=0; i<5000000; ++i )
	{
		util_bit.SetBit(i%128);
		util_bit.ClearBit(i%128);
		util_bit.TestBit(i%128);
		util_bit.Clear();
	}
	END_PERFORMANCE(1)
	cout <<"-------- Util::BitField   integrated testing "<<endl; 
	
#endif

	// test performance of hash of Util::String
#if 0

	START_PERFORMANCE(1)
	for (int i=0; i<100000; ++i )
	{
		size_t test_str = Util::String::FromInt(i*3).HashCode();
	}
	END_PERFORMANCE(1)
	cout <<"-------- Util::String   HashCode()"<<endl; 

	START_PERFORMANCE(2)
	for (int i=0; i<100000; ++i )
	{
		size_t test_str = Util::String::FromInt(i*3).hash_code_std_style();
	}
	END_PERFORMANCE(2)
	cout <<"-------- Util::String  hash_code_std_style() "<<endl; 


#endif

#if 0
	Util::Array<int> arr;
	std::vector<int> vec;

	START_PERFORMANCE(1)
	for ( int i=100000; i>0; --i )
	{
		arr.Resize(i);
	}
	END_PERFORMANCE(1)
	cout <<"-------- Util::Array Resize()"<<endl; 

	START_PERFORMANCE(2)
	for ( int i=100000; i>0; --i )
	{
		vec.resize(i);
	}
	END_PERFORMANCE(2)
	cout <<"-------- std::vector resize() "<<endl; 

#endif

	//test logManager
#if 0

	LogManager* logMgr = new LogManager();

	logMgr->createLog("testLog.log", true, true);

	LogManager::Instance()->stream()
		.write("dfafdfa [%d] dfdafdaffdaf   %d   dsfdsf",454,97899);

	LogManager::Instance()->stream()
		.write("dRRRRa [%d] dfdRRRRRaf   %d   dsfdsf",454,97899);

	cout<< "ggg" << LogManager::Instance()->getDefaultLog()->getName().AsCharPtr() <<endl;


#endif
	//test std style methods in String
#if 1
	std::string std_str("test std style methods in String     methods xx methods xx methods xx methods");
	Util::String util_str("test std style methods in String     methods xx methods xx methods xx methods");

	PERFORMANCE_BEGIN();
	for (int i=0; i<50000; ++i)
	{
		std_str.substr(2,7);
	}
	std::cout<<std_str.substr(2,7).c_str()<<std::endl;
	PERFORMANCE_END("std::string substr");

	PERFORMANCE_BEGIN();
	for (int i=0; i<50000; ++i)
	{
		util_str.SubString(2,7);
	}
	std::cout<<util_str.SubString(2,7).AsCharPtr()<<std::endl;
	PERFORMANCE_END("Util::String SubString");
	//-------------------------------
	std::cout<<std::endl;

	const char* str = "methods";
	PERFORMANCE_BEGIN();
	for (int i=0; i<50000; ++i)
	{
		std_str.find(str, 2, 6);
	}
	std::cout<< std_str.find(str, 2, 6) <<std::endl;
	PERFORMANCE_END("std::string find");

	PERFORMANCE_BEGIN();
	for (int i=0; i<50000; ++i)
	{
		util_str.find(str, 2, 6);
	}
	std::cout<< util_str.find(str, 2, 6) <<std::endl;
	PERFORMANCE_END("Util::String find");

	//-------------------------------
	std::cout<<std::endl;

	PERFORMANCE_BEGIN();
	for (int i=0; i<50000; ++i)
	{
		replaceAll(std_str,"methods","MONKEY");
		replaceAll(std_str,"MONKEY","methods");
	}
	replaceAll(std_str,"methods","MONKEY");
	std::cout<< std_str.c_str() <<std::endl;
	PERFORMANCE_END("std::string replace");

	PERFORMANCE_BEGIN();
	for (int i=0; i<50000; ++i)
	{
		util_str.SubstituteString("methods","MONKEY");
		util_str.SubstituteString("MONKEY","methods");
	}
	util_str.SubstituteString("methods","MONKEY");
	std::cout<< util_str.AsCharPtr() <<std::endl;
	PERFORMANCE_END("Util::String SubstituteString");

#endif
	cin.ignore();
}